﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Xml.Serialization;
using System.Data;

namespace two_ado
{
     public class DBhelper
    {
        private string sql= "SELECT * FROM userinfo";
        private string conn= "server = . ; database =DBtest ; uid = sa ; pwd = 20000624...";

        public static void GetUserinfo()
        {

            //连接数据库
            string connStr = "server = . ; database =DBtest ; uid = sa ; pwd = 20000624...";
            //声明SqlConnection
            SqlConnection conn = new SqlConnection(connStr);
            //打开连接
            conn.Open();
            //声明查询SqlCommand
            string sql = "SELECT * FROM userinfo";
            SqlCommand cmd = new SqlCommand(sql, conn);
            //执行查询语句
            SqlDataReader sdr = cmd.ExecuteReader();
            Console.WriteLine("编号" + "\t" + "名称" + "\t" + "年龄" + "\t" + "性别");
            while (sdr.Read())
            {
                Console.WriteLine(sdr["Id"] + "\t" + sdr["Name"] + "\t" + sdr["Age"] + "\t" + sdr["Sex"]);
            }
            conn.Close();
        }
        public void PrintUserinfo()
        {
            SqlDataAdapter dat = new SqlDataAdapter(sql, conn);
            DataTable dte = new DataTable();
            dat.Fill(dte);
            Console.WriteLine("编号" + "\t" + "名称" + "\t" + "年龄" + "\t" + "性别");
            for (int i = 0; i < dte.Rows.Count; i++)
            {
                Console.WriteLine(dte.Rows[i]["Id"] + "\t" + dte.Rows[i]["Name"] + "\t" + dte.Rows[i]["Age"] + "\t" + dte.Rows[i]["Sex"]);

            }

        }

    }
}
